/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * KlassenBeheerMove.java
 *
 * Created on 6-jun-2011, 17:12:08
 */

package view;

import connections.Query;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.List;
import main.ApplicatieMain;

/**
 *
 * @author Jeroen
 */
public class KlassenBeheerMove extends javax.swing.JFrame {
    private Query query;
    private ApplicatieMain applicatieMain;
    private List<String> klassenLijst;
    private String leerlingNaam;

    /** Creates new form KlassenBeheerMove */
    public KlassenBeheerMove(String leerlingNaam){
        applicatieMain = ApplicatieMain.getInstance();
        query = applicatieMain.getQuery();
        this.leerlingNaam = leerlingNaam;

        initComponents();
        initKlassenLijst();
        
        // Hoe groot is het scherm?
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (dim.width - this.getSize().width) /2;
        int y = (dim.height - this.getSize().height) /2;
        this.setLocation(x, y);
        this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
        this.setTitle("Leerling Verplaatsen");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        listKlassen = new javax.swing.JList();
        btnVerplaats = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        listKlassen.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listKlassenValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(listKlassen);

        btnVerplaats.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnVerplaats.setForeground(new java.awt.Color(0, 102, 0));
        btnVerplaats.setText("Verplaats");
        btnVerplaats.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnVerplaats.setEnabled(false);
        btnVerplaats.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVerplaatsActionPerformed(evt);
            }
        });

        btnCancel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnCancel.setForeground(new java.awt.Color(255, 0, 0));
        btnCancel.setText("Cancel");
        btnCancel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        jLabel1.setText("Verplaats leerling naar klas:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnVerplaats, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jLabel1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnVerplaats, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void initKlassenLijst() {
        klassenLijst = query.getKlassenLijst(applicatieMain.getBrinNummer());
        listKlassen.setListData(klassenLijst.toArray());
    }

    private void listKlassenValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listKlassenValueChanged
        if (listKlassen.getSelectedIndex() != - 1) {
            btnVerplaats.setEnabled(true);
        } else {
            btnVerplaats.setEnabled(false);
        }
}//GEN-LAST:event_listKlassenValueChanged

    private void btnVerplaatsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVerplaatsActionPerformed
        String selectedKlas = (String)listKlassen.getSelectedValue();
        query.setUserKlas(selectedKlas, leerlingNaam);
        System.out.println("USERKLAS:" + selectedKlas);
        applicatieMain.getKlasBeheer().initKlassenLijst();
        applicatieMain.getKlasBeheer().initLeerlingLijst();
        this.dispose();
}//GEN-LAST:event_btnVerplaatsActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.dispose();
}//GEN-LAST:event_btnCancelActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnVerplaats;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList listKlassen;
    // End of variables declaration//GEN-END:variables

}
